<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:productAttributeInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:productAttributeInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:productAttributeInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:productAttributeInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productAttributeInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="商品属性" align="center" prop="productAttributeCategoryId" />
      <el-table-column label="属性是否可选" align="center" prop="selectType" >
        <template slot-scope="scope">
          <span v-if="scope.row.selectType === 0">唯一</span>
          <span v-if="scope.row.selectType === 1">单选</span>
          <span v-if="scope.row.selectType === 2">多选</span>
        </template>
      </el-table-column>
      <el-table-column label="属性录入方式" align="center" prop="inputType" >
        <template slot-scope="scope">
          <span v-if="scope.row.inputType === 0">手工录入</span>
          <span v-if="scope.row.inputType === 1">从列表中选取</span>
        </template>
      </el-table-column>
      <el-table-column label="可选值列表，以逗号隔开" align="center" prop="inputList" />
      <el-table-column label="排序字段" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:productAttributeInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:productAttributeInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="商品属性分类" prop="productAttributeCategoryId">
          <el-select v-model="form.productAttributeCategoryId" placeholder="请选择活动区域">
            <el-option v-for="i in Attribute" :label="i.productAttributeCategoryId" :value="i.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类筛选样式" prop="filterType">
          <template>
            <el-radio v-model="form.filterType" :label="1">普通</el-radio>
            <el-radio v-model="form.filterType" :label="2">颜色</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="能否进行检索" prop="searchType">
          <template>
            <el-radio v-model="form.searchType" :label="0">不需要进行检索</el-radio>
            <el-radio v-model="form.searchType" :label="1">关键字检索</el-radio>
            <el-radio v-model="form.searchType" :label="2">范围检索</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="商品属性关联" prop="relatedStatus">
          <el-radio v-model="form.relatedStatus" :label="0">不关联</el-radio>
          <el-radio v-model="form.relatedStatus" :label="1">关联</el-radio>
        </el-form-item>
        <el-form-item label="属性是否可选" prop="selectType">
          <el-radio v-model="form.selectType" :label="0">唯一</el-radio>
          <el-radio v-model="form.selectType" :label="1">单选</el-radio>
          <el-radio v-model="form.selectType" :label="2">多选</el-radio>
        </el-form-item>
        <el-form-item label="属性值的录入方式" prop="inputType">
          <el-radio v-model="form.inputType" :label="0">手工录入</el-radio>
          <el-radio v-model="form.inputType" :label="1">从列表中选取</el-radio>
        </el-form-item>
        <el-form-item label="属性值可选值列表" prop="inputList">
          <el-input v-model="form.inputList" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否支持手动新增" prop="handAddStatus">
          <el-radio v-model="form.handAddStatus" :label="0">不支持</el-radio>
          <el-radio v-model="form.handAddStatus" :label="1">支持</el-radio>
        </el-form-item>
        <el-form-item label="排序属性" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="属性的类型" prop="type">
          <el-radio v-model="form.type" :label="0">属性</el-radio>
          <el-radio v-model="form.type" :label="1">规格</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductAttributeInfo, getProductAttributeInfo, delProductAttributeInfo, addProductAttributeInfo, updateProductAttributeInfo } from "@/api/product/productAttributeInfo";
import {listAttributeCategoryInfo} from "../../../api/product/attributeCategoryInfo";

export default {
  name: "ProductAttributeInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品属性表格数据
      productAttributeInfoList: [],
      Attribute:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:this.$route.query.id,
        type: this.$route.query.type
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "商品名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAttributeById();
  },
  watch: {
    $route() {
      this.queryParams.id = this.$route.query.id;

      this.queryParams.type = this.$route.query.type;


      listProductAttributeInfo(this.queryParams).then(response => {
        this.productAttributeInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      console.log(this.queryParams)
      //监听到对象后调用请求方法
      this.getList();
    }
  },

  methods: {
    //下拉框查询商品属性分类id
    getAttributeById() {
      listAttributeCategoryInfo(this.Attribute).then(response => {
        this.Attribute = response.rows;
      });
    },
    /** 查询商品属性列表 */
    getList() {
      this.queryParams.productAttributeCategoryId=this.$route.query.productAttributeCategoryId
      this.loading = true;
      listProductAttributeInfo(this.queryParams).then(response => {
        this.productAttributeInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productAttributeCategoryId: null,
        name: null,
        selectType: null,
        inputType: null,
        inputList: null,
        sort: null,
        filterType: null,
        searchType: null,
        relatedStatus: null,
        handAddStatus: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductAttributeInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductAttributeInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductAttributeInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品属性编号为"' + ids + '"的数据项？').then(function() {
        return delProductAttributeInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/productAttributeInfo/export', {
        ...this.queryParams
      }, `productAttributeInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
